﻿#include "CommandProcessor.h"

#ifdef _WIN32
#include <Windows.h>
extern "C" {
	__declspec(dllexport) unsigned long NvOptimusEnablement = 0x00000001;
}

std::string wstringToString(std::wstring wstr) {
	std::string result;
	//获取缓冲区大小，并申请空间，缓冲区大小按字节计算
	int len = WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
	char* buffer = new char[len + 1];
	//宽字节编码转换成多字节编码  
	WideCharToMultiByte(CP_UTF8, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
	buffer[len] = '\0';
	//删除缓冲区并返回值  
	result.append(buffer);
	delete[] buffer;
	return result;
}

int wmain(int argc, wchar_t* argv[]) {
	system("chcp 65001");
	setlocale(LC_ALL, ".UTF-8");
	std::string* argv_str = new std::string[argc - 1];
	for (int i = 0; i < argc - 1; i++) {
		argv_str[i] = wstringToString(argv[i + 1]);
	}
	try {
		process_command(argc - 1, argv_str);
	}
	catch (const char* e) {
		std::cerr << "fatal error: " << e << std::endl;
		std::cerr << std::endl << "发生错误，无法完成渲染。" << std::endl;
		return -1;
	}
	return 0;
}
#else
int main(int argc, char* argv[]) {
	std::string* argv_str = new std::string[argc - 1];
	for (int i = 0; i < argc - 1; i++) {
		argv_str[i] = argv[i + 1];
	}
	//setenv("PATH", "/usr/local/cuda-12.0/bin", 1);
	try {
		process_command(argc - 1, argv_str);
	}
	catch (const char* e) {
		std::cerr << "Fatal Error: " << e << std::endl;
		std::cerr << std::endl << "发生错误，无法完成渲染。" << std::endl;
		return -1;
	}
}
#endif